package com.example.makerspace.manager;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.example.makerspace.dao.NewsDAO;
import com.example.makerspace.domain.NewsDO;
import com.example.makerspace.dto.*;
import com.example.makerspace.enums.ResultCodeEnum;
import com.example.makerspace.utils.DateUtils;
import com.example.makerspace.utils.FileUtils;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Lists;
import org.apache.commons.collections4.MapUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

import static com.example.makerspace.service.impl.AdminServiceImpl.managerMap;

@Component
public class NewsManager extends BaseManager<NewsDO, NewsDTO>{

    private final static String NEWS_PICTURE_PATH_PREFIX = "news/";

    private final static List<Map<String, String>> FUZZY_QUERY_PARAMS = Lists.newArrayList(
            ImmutableMap.<String, String>builder().put(QUERY_FIELD, "title").put(QUERY_TYPE, QUERY_TYPE_MATCH_PHRASE).build(),
            ImmutableMap.<String, String>builder().put(QUERY_FIELD, "content").put(QUERY_TYPE, QUERY_TYPE_MATCH_PHRASE).build(),
            ImmutableMap.<String, String>builder().put(QUERY_FIELD, "rich_text").put(QUERY_TYPE, QUERY_TYPE_MATCH_PHRASE).build()
    );
  private  final  static  String   testHttpPrefix="https://119.45.199.13:3035/static/";
   private  final  static  String  devHttpPrefix="https://nism.ytcata.com:443/static/";

    @Resource
    private NewsDAO newsDAO;

    @PostConstruct
    public void init(){
        this.baseDAO = newsDAO;
        managerMap.put("news", this);
    }

    public Result addNews(NewsDTO newsDTO){
        NewsDO oldNewsDO = newsDAO.selectByNewsTitle(newsDTO);
        if(oldNewsDO != null){
            return Result.error(ResultCodeEnum.NEWS_EXIST);
        }
        NewsDO newsDO = new NewsDO();
        BeanUtils.copyProperties(newsDTO, newsDO);
        Date parse = DateUtils.parse(newsDTO.getShowTime(), DateUtils.YYYY_MM_DD);
        newsDO.setShowTime(parse);
        return addDO(newsDO);
    }

    public Result updateNews(NewsDTO newsDTO){
        NewsDO newsDO = newsDAO.selectById(newsDTO.getId());
        if(newsDO == null){
            return Result.error(ResultCodeEnum.NEWS_NOT_EXIST);
        }
        Optional.ofNullable(newsDTO.getTitle()).ifPresent(newsDO::setTitle);
        Optional.ofNullable(newsDTO.getContent()).ifPresent(newsDO::setContent);
        Optional.ofNullable(newsDTO.getRichText()).ifPresent(newsDO::setRichText);
        Optional.ofNullable(newsDTO.getStatus()).ifPresent(newsDO::setStatus);
        Optional.ofNullable(newsDTO.getShowTime()).ifPresent(showTime ->{
            Date parse = DateUtils.parse(showTime, DateUtils.YYYY_MM_DD);
            newsDO.setShowTime(parse);
        });
        return updateDO(newsDO);
    }




    public Result latestNews(Integer num) {
        List<NewsDO> newsDOS = newsDAO.selectLatest(num);
        List<NewsDTO> collect = Optional.ofNullable(newsDOS).orElse(Collections.emptyList()).stream()
                .map(newsDO -> {
                    NewsDTO newsDTO = new NewsDTO();
                    newsDTO.setId(newsDO.getId());
                    newsDTO.setTitle(newsDO.getTitle());
                    newsDTO.setShowTime(DateUtils.format(newsDO.getShowTime(), DateUtils.YYYY_MM_DD));
                    return newsDTO;
                }).collect(Collectors.toList());
        return Result.success(collect);

    }

    public Result detail(NewsDTO newsDTO) {
        NewsDO newsDO = newsDAO.selectById(newsDTO.getId());
        if(newsDO == null){
            return Result.error(ResultCodeEnum.NEWS_NOT_EXIST);
        }
        return Result.success(convert2DTO(newsDO));
    }

    @Override
    public PageResult list(PageRequest pageRequest){
        Map<String, Object> params = pageRequest.getParams();
        Pagination pagination = pageRequest.getPagination();
        String fuzzy = MapUtils.getString(params, "fuzzy");
        if(StringUtils.isBlank(fuzzy)){
            return super.list(pageRequest);
        }
        return search(null, null, FUZZY_QUERY_PARAMS, fuzzy, pagination, NewsDO.class);
    }


    public Result newsListByTime(PageRequest pageRequest) {
        List<NewsDO> newsDOS = newsDAO.selectLatest((int) pageRequest.getPagination().getSize());
        List<NewsDTO> collect = Optional.ofNullable(newsDOS)
                .orElse(Collections.emptyList()).stream().map(this::convert2DTO).collect(Collectors.toList());
        return Result.success(collect);
    }
}
